package com.huwaiwai001.producer_consumer;

/**
 * Created by hww on 2019/1/15 5:54 PM.
 */
public class ConditionTest {

    private Object condition = new Object();


    public static void main(String [] args) throws InterruptedException {

        Object c = new ConditionTest().condition;
        Thread t = new Thread(() -> {
            try {
                System.out.println("开始阻塞");
                // 需要先持有对象 才能操作对象
                synchronized (c) {
                    c.wait();
                }
                System.out.println("唤醒");
            } catch (InterruptedException e) {
                System.out.println("异常");
            }
        });
        t.start();

        Thread t2 = new Thread(() -> {
            System.out.println("开始唤醒");
            synchronized (c) {
                c.notify();
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("唤醒成功");
        });
        t2.start();
        Thread.sleep(5000L);

    }

}
